package com.isay.frameworklib.utils;

import android.text.TextUtils;

/**
 * Created by cywu4 on 2018/12/18.
 */
public class TimeUtils {


    /**
     * 将时分秒转为秒数
     *
     * @param time xx：xx：xx
     * @return
     */
    public static long toSecond(String time) {
        if (!TextUtils.isEmpty(time)) {
            String[] arr = time.split(":");
            if (arr.length == 3) {
                int h = Integer.parseInt(arr[0]) * 60 * 60;
                int m = Integer.parseInt(arr[1]) * 60 * 60;
                int s = Integer.parseInt(arr[2]) * 60 * 60;
                return h + m + s;
            }
        }
        return -1;
    }


    /**
     * 将秒数转为时分秒
     *
     * @param second
     * @return
     */
    public static String toTime(long second) {
        long h = 0;
        long d = 0;
        long s = 0;
        long temp = second % 3600;
        if (second > 3600) {
            h = second / 3600;
            if (temp != 0) {
                if (temp > 60) {
                    d = temp / 60;
                    if (temp % 60 != 0) {
                        s = temp % 60;
                    }
                } else {
                    s = temp;
                }
            }
        } else {
            d = second / 60;
            if (second % 60 != 0) {
                s = second % 60;
            }
        }
        String result = null;
        if (h < 10) {
            result = "0" + h + "时";
        } else {
            result = h + "时";
        }
        if (d < 10) {
            result += "0" + d + "分";
        } else {
            result += d + "分";
        }
        if (s < 10) {
            result += "0" + s + "秒";
        } else {
            result += s + "秒";
        }
        return result;//h + "时" + d + "分" + s + "秒";
    }

}
